/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package jmmos.gameserver.model.actor.instance;

import java.util.List;
import java.util.logging.Level;

import javolution.util.FastList;

import jmmos.gameserver.model.MMOSpawn;
import jmmos.gameserver.model.actor.MMOCharacter;
import jmmos.gameserver.model.actor.MMOTower;
import jmmos.gameserver.model.actor.templates.MMONpcTemplate;

/**
 * Class for Control Tower instance.
 */
public class MMOControlTowerInstance extends MMOTower
{
	private List<MMOSpawn> _guards;
	
	public MMOControlTowerInstance(int objectId, MMONpcTemplate template)
	{
		super(objectId, template);
		setInstanceType(InstanceType.MMOControlTowerInstance);
	}
	
	@Override
	public boolean doDie(MMOCharacter killer)
	{
		if (getCastle().getSiege().getIsInProgress())
		{
			getCastle().getSiege().killedCT(this);
			
			if ((_guards != null) && !_guards.isEmpty())
			{
				for (MMOSpawn spawn : _guards)
				{
					if (spawn == null)
					{
						continue;
					}
					try
					{
						spawn.stopRespawn();
						// spawn.getLastSpawn().doDie(spawn.getLastSpawn());
					}
					catch (Exception e)
					{
						_log.log(Level.WARNING, "Error at MMOControlTowerInstance", e);
					}
				}
				_guards.clear();
			}
		}
		return super.doDie(killer);
	}
	
	public void registerGuard(MMOSpawn guard)
	{
		getGuards().add(guard);
	}
	
	private final List<MMOSpawn> getGuards()
	{
		if (_guards == null)
		{
			synchronized (this)
			{
				if (_guards == null)
				{
					_guards = new FastList<>();
				}
			}
		}
		return _guards;
	}
}
